//
//  HTLeftUserMenuView.m
//  HT_aigongzuo
//
//  Created by 周昭 on 16/9/1.
//  Copyright © 2016年 HT_Technology. All rights reserved.
//

#import "HTLeftUserMenuView.h"
#import "HTMenuHeaderView.h"
#import "HTMenuUserModel.h"
#import "HTLeftMenuCell.h"
#import "Masonry.h"

#define KScreenWidth    [UIScreen mainScreen].bounds.size.width
#define KScreenHeight   [UIScreen mainScreen].bounds.size.height
@interface HTLeftUserMenuView()<UITableViewDataSource,UITableViewDelegate,HTMenuHeaderViewDelegate,HTMenuFooterViewDelegate>
/**
 *  模型数组
 */
@property (nonatomic, strong) NSMutableArray *menuModelArr;
/**
 *  headerView
 */
@property (nonatomic, weak) HTMenuHeaderView *headerView;
/**
 *  tableView
 */
@property (nonatomic, weak) UITableView *tableView;
/**
 *  footerView
 */
@property (nonatomic, weak) HTMenuFooterView *footerView;
@end

@implementation HTLeftUserMenuView
- (NSMutableArray *)menuModelArr
{
    if (_menuModelArr == nil) {
        _menuModelArr = [NSMutableArray array];
//        NSArray *titleArr = @[@"收藏",@"联系人",@"附近",@"互动",@"热门",@"定位",@"退出登录"];
//        NSArray *iconArr = @[@"app",@"find_people",@"near",@"game_center",@"hot_status",@"cast",@"more"];
//        for (int i = 0; i < titleArr.count; i++) {
//            HTMenuUserModel *model = [HTMenuUserModel menuWithIcon:iconArr[i] title:titleArr[i]];
//            [_menuModelArr addObject:model];
//        }
        NSUserDefaults *userDf = [NSUserDefaults standardUserDefaults];
        NSDictionary *configDic = [userDf valueForKey:@"systemConfig"];
        NSArray *menuLinkArr = configDic[@"menu"];
        if (!IsArrEmpty(menuLinkArr)) {
            for (NSDictionary *linkDict in menuLinkArr) {
                HTMenuUserModel *model = [HTMenuUserModel menuWithIcon:linkDict[@"menuImg"] title:linkDict[@"menuTitle"] isWebIcon:YES linkUrlStr:linkDict[@"menuUrl"]];
                [_menuModelArr addObject:model];
            }
        }
    }
    return _menuModelArr;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpSubViews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder
{
    if (self = [super initWithCoder:decoder]) {
        [self setUpSubViews];
    }
    return self;
}

/**
 *  初始化所有子控件&进行一次性设置
 */
- (void)setUpSubViews
{
    // 1.设置头部的view
    HTMenuHeaderView *headerView = [[HTMenuHeaderView alloc] init];
    headerView.delegate = self;
    self.headerView = headerView;
    [self addSubview:headerView];
    
    // 0.创建tableView
    UITableView *tableView = [[UITableView alloc] init];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableFooterView = [[UIView alloc] init];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:tableView];
    self.tableView = tableView;
    
    // 2.给tableView设置约束
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(KScreenHeight == 568 ? 128 : 180, 0, 60, 0));
    }];
    
    // 3.创建底部的view
    HTMenuFooterView *footerView = [HTMenuFooterView customFooterView];
    footerView.backgroundColor = [UIColor whiteColor];
    footerView.delegate = self;
    [self addSubview:footerView];
    self.footerView = footerView;
}

/**
 *  拿到真实尺寸进行设置
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.headerView.frame = CGRectMake(0, 0, self.frame.size.width, KScreenHeight == 568 ? 128 : 180);
    
    self.footerView.frame = CGRectMake(0, self.frame.size.height - 65, self.frame.size.width, 65);
}

#pragma mark - method
- (void)updateHeaderInfo
{
    [self.headerView setUpUserInfoData];
}

#pragma mark - footerView代理方法
- (void)menuFooterView:(HTMenuFooterView *)footerView didClickBtnWithType:(HTMenuFooterBtnType)type
{
    if ([self.delegate respondsToSelector:@selector(leftUserMenuFooterViewDidClickBtnWithType:)]) {
        [self.delegate leftUserMenuFooterViewDidClickBtnWithType:type];
    }
}

#pragma mark - headerView代理方法
- (void)menuHeaderViewDidClickBgView
{
    if ([self.delegate respondsToSelector:@selector(leftUserMenuHeaderViewDidClickBgView)]) {
        [self.delegate leftUserMenuHeaderViewDidClickBgView];
    }
}

#pragma mark - tableView代理和数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.menuModelArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return KScreenHeight == 568 ? 44 : 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HTLeftMenuCell *cell = [HTLeftMenuCell cellWithTableView:tableView];
    cell.indexPath = indexPath;
    cell.model = [self.menuModelArr objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HTMenuUserModel *model = [self.menuModelArr objectAtIndex:indexPath.row];
    if([self.delegate respondsToSelector:@selector(leftUserMenuViewClick:model:)]){
        [self.delegate leftUserMenuViewClick:indexPath.row model:model];
    }
}

@end
